//--------------------------------------------E_F_G_estout_esttab_appendix.do
// Appendix E F and G
// --------------------------------------------------------------------------
version 15 // ensure compatibility
clear all // start in a clean state
cls // clear the results window
set more off // show all output, dont wait if screen full
set seed 12345 // see note below
set rmsg on // displays how time to execute command s
cd "C:\Users\wachb\OneDrive - UvA\WInequality\Replicate_Analysis"
capture log close // closes any log file if currently open
log using E_F_G_estout_esttab_appendix, replace text

//open the data
use "C:\Users\wachb\OneDrive - UvA\WInequality\Replicate_Analysis\countryxyzcorr.dta"

//The estout package provides tools for making regression tables. 
//eststo stores a copy of the active estimation results for later tabulation. (It is an alternative to official Stata's estimates store.)
//esttab is a command to make publication-style tables.
//SW stands for stepwise, C stands for extra controls, and R stands for Robustness check.

label var meanUDS "UDS"
label var RGTQ "Social Trust"
label var MeanGiniSWIID "Gini Income"
label var MeanGDP "GDP"
label var GiniWealth "Gini Wealth"
label var prepr "Trust in Representative Institutions"
label var pimpl "Trust in Implementing Institutions"
label var z_meanUDS "UDS"
label var z_logRGTQ "Social Trust"
label var z_MeanGiniSWIID "Gini Income"
label var z_logMeanGDP "GDP"
label var z_GiniWealth "Gini Wealth"
label var z_prepr "Trust in Representative Institutions"
label var z_pimpl "Trust in Implementing Institutions"
label var t "Time Interval"
label var z_prepr_wg "Trust in Representative Institutions (excl. item on government)"

order ISO3166_2-logRGTQ GTQmis preprimplmis prepr_wg GTQmis preprimplmis ///
z_GiniWealth-z_meanUDS z_logMeanGDP z_logRGTQ z_prepr z_pimpl z_prepr_wg

//Appendix E1: GT models from main analysis with extra controls 
eststo clear
local time i.t
local robust cluster(ISO3166_1_num1)
local inter1 c.z_GiniWealth##c.z_logMeanGDP
local inter2 c.z_GiniWealth##c.z_meanUDS
local inter3 c.z_MeanGiniSWIID##c.z_logMeanGDP
local inter4 c.z_MeanGiniSWIID##c.z_meanUDS
/*model 1*/eststo: quie reg z_logRGTQ z_GiniWealth z_MeanGiniSWIID z_prepr z_pimpl `time', `robust'
/*model 2*/eststo: quie reg z_logRGTQ z_GiniWealth z_MeanGiniSWIID z_logMeanGDP z_meanUDS z_prepr z_pimpl `time', `robust'
/*model 3*/eststo: quie reg z_logRGTQ z_MeanGiniSWIID `inter1' `inter2' z_prepr z_pimpl `time', `robust'
/*model 4*/eststo: quie reg z_logRGTQ z_GiniWealth `inter3' `inter4' z_prepr z_pimpl `time', `robust'

esttab using E1_GTQ_C.rtf, order(z_GiniWealth z_MeanGiniSWIID z_prepr z_pimpl z_logMeanGDP z_meanUDS ///
c.z_GiniWealth#c.z_logMeanGDP c.z_GiniWealth#c.z_meanUDS c.z_MeanGiniSWIID#c.z_logMeanGDP c.z_MeanGiniSWIID#c.z_meanUDS) ///
b(3) se(3) r2 varwidth(17) compress ///
coeflabels (z_prepr "Trust in Repr. Inst." z_pimpl "Trust in Impl. Inst.") label nobaselevels nogaps ///
noomitted interaction(" \f1 x \f0") title(Pooled cross-sectional models of Generalized ///
Trust controlling for both forms of Institutional Trust) nonumbers mtitles("Model 1 Coefficient (SE)" "Model 2 Coefficient (SE)" "Model 3 Coefficient (SE)" "Model 4 Coefficient (SE)") ///
nonotes addnotes("Note.---SE = Robust standard errors clustered at country level; Repr. = Representative; Inst. = Institutions; Impl. = Implementing; GDP = Gross Domestic Product per capita at purchasing power parity 2017 international $; UDS = Unified Democracy Score; GDP and Social Trust transformed using natural logarithm, all variables z-standardized." "* p < 0.05." "** p < 0.01." "*** p < 0.001.") ///
fonttbl(\f0\fnil Times New Roman;\f1\fnil Arial;) substitute("\fs20" "\fs24" "Interval=" "Interval " "Observations" "\i N" " p " "{\i p }" "*" "{\super *}") replace
//manually removed line above N

//Appendix E2 E3: institutional trust models main analysis with extra controls
eststo clear
foreach var of varlist(z_prepr z_pimpl){ 
	local time i.t	
	local robust cluster(ISO3166_1_num1)
	local inter1 c.z_GiniWealth##c.z_logMeanGDP
	local inter2 c.z_GiniWealth##c.z_meanUDS
	local inter3 c.z_MeanGiniSWIID##c.z_logMeanGDP
	local inter4 c.z_MeanGiniSWIID##c.z_meanUDS
	eststo clear
	/*model 1*/eststo: quie reg `var' z_GiniWealth z_MeanGiniSWIID z_logRGTQ `time', `robust'
	/*model 2*/eststo: quie reg `var' z_GiniWealth z_MeanGiniSWIID z_logMeanGDP z_meanUDS z_logRGTQ `time', `robust'
	/*model 3*/eststo: quie reg `var' z_MeanGiniSWIID `inter1' `inter2' z_logRGTQ `time', `robust'
	/*model 4*/eststo: quie reg `var' z_GiniWealth `inter3' `inter4' z_logRGTQ `time', `robust'
	local name = subinstr("`var'", "z_", "",.)
	local name2 = subinstr("`name'", "prepr", "2_prepr",.)
	local name3 = subinstr("`name2'", "pimpl", "3_pimpl",.)
	esttab using E`name3'_C.rtf, order(z_GiniWealth z_MeanGiniSWIID z_logRGTQ z_logMeanGDP z_meanUDS ///	
	c.z_GiniWealth#c.z_logMeanGDP c.z_GiniWealth#c.z_meanUDS c.z_MeanGiniSWIID#c.z_logMeanGDP c.z_MeanGiniSWIID#c.z_meanUDS) ///
	b(3) se(3) r2 varwidth(17) compress label nobaselevels nogaps ///
	noomitted interaction(" \f1 x \f0") title(Pooled cross-sectional models of `"`: var label `var''"' controlling for Social Trust) ///
	nonumbers mtitles("Model 1 Coefficient (SE)" "Model 2 Coefficient (SE)" "Model 3 Coefficient (SE)" "Model 4 Coefficient (SE)") ///
	nonotes addnotes("Note.---SE = Robust standard errors clustered at country level; GDP = Gross Domestic Product per capita at purchasing power parity 2017 international $; UDS = Unified Democracy Score; GDP and Social Trust transformed using natural logarithm, all variables z-standardized." "* p < 0.05." "** p < 0.01." "*** p < 0.001.") ///
	fonttbl(\f0\fnil Times New Roman;\f1\fnil Arial;) substitute("\fs20" "\fs24" "Interval=" "Interval " "Observations" "\i N" " p " "{\i p }" "*" "{\super *}") replace
}
//manually removed line above N, and changed doc #

//Appendix F: trust in representative institutions without the government item (repeat analysis in the main text)
//z_prepr_wg
eststo clear
local control1 z_logMeanGDP z_meanUDS
local time i.t
local robust cluster(ISO3166_1_num1)
local inter1 c.z_GiniWealth##c.z_logMeanGDP
local inter2 c.z_GiniWealth##c.z_meanUDS
local inter3 c.z_MeanGiniSWIID##c.z_logMeanGDP
local inter4 c.z_MeanGiniSWIID##c.z_meanUDS
/*model 1*/eststo: quietly reg z_prepr_wg z_GiniWealth z_MeanGiniSWIID `time', `robust'
/*model 2*/eststo: quietly reg z_prepr_wg z_GiniWealth z_MeanGiniSWIID `control1' `time', `robust'
/*model 3*/eststo: quietly reg z_prepr_wg z_MeanGiniSWIID `inter1' `inter2' `time', `robust'
/*model 4*/eststo: quietly reg z_prepr_wg z_GiniWealth `inter3' `inter4' `time', `robust'
esttab using F1_Prepr_wg_R.rtf, order(z_GiniWealth z_MeanGiniSWIID z_logMeanGDP z_meanUDS ///
c.z_GiniWealth#c.z_logMeanGDP c.z_GiniWealth#c.z_meanUDS c.z_MeanGiniSWIID#c.z_logMeanGDP ///
c.z_MeanGiniSWIID#c.z_meanUDS) b(3) se(3) r2 varwidth(17) compress label nobaselevels nogaps ///
noomitted interaction(" \f1 x \f0") /*onecell*/ title(Pooled cross-sectional models of Trust in Representative Institutions (excl. item on government)) ///
nonumbers mtitles("Model 1 Coefficient (SE)" "Model 2 Coefficient (SE)" "Model 3 Coefficient (SE)" "Model 4 Coefficient (SE)") ///
nonotes addnotes("Note.---SE = Robust standard errors clustered at country level; GDP = Gross Domestic Product per capita at purchasing power parity 2017 international $; UDS = Unified Democracy Score; GDP transformed using natural logarithm, all variables z-standardized." "* p < 0.05." "** p < 0.01." "*** p < 0.001.") ///
fonttbl(\f0\fnil Times New Roman;\f1\fnil Arial;) substitute("\fs20" "\fs24" "Interval=" "Interval " "Observations" "\i N" " p " "{\i p }" "*" "{\super *}") replace
//manually removed line above N

//Appendix G: stepwise 
foreach var of varlist(z_logRGTQ z_prepr z_pimpl){
	eststo clear
	local control1 z_logMeanGDP z_meanUDS
	local time i.t
	local robust cluster(ISO3166_1_num1)
	local inter1 c.z_GiniWealth##c.z_logMeanGDP
	local inter2 c.z_GiniWealth##c.z_meanUDS
	local inter3 c.z_MeanGiniSWIID##c.z_logMeanGDP
	local inter4 c.z_MeanGiniSWIID##c.z_meanUDS
	local inter5 c.z_GiniWealth##c.z_MeanGiniSWIID
	/*model 1*/eststo: quietly reg `var' z_GiniWealth `time', `robust'
	/*model 2*/eststo: quietly reg `var' z_MeanGiniSWIID `time', `robust'
	/*model 3*/eststo: quietly reg `var' z_GiniWealth `control1' `time', `robust'
	/*model 4*/eststo: quietly reg `var' z_MeanGiniSWIID `control1' `time', `robust'
	/*model 5*/eststo: quietly reg `var' `inter1' `inter2' `time', `robust'
	/*model 6*/eststo: quietly reg `var' `inter3' `inter4' `time', `robust'
	/*model 7*/eststo: quietly reg `var' `inter1' `inter3' z_meanUDS `time', `robust'
	/*model 8*/eststo: quietly reg `var' `inter2' `inter4' z_logMeanGDP `time', `robust'
	/*model 9*/eststo: quietly reg `var' `inter5' `time', `robust'
	/*model 10*/eststo: quietly reg `var' `inter5' `control1' `time', `robust'
	local name = subinstr("`var'", "z_", "",.)
	local name2 = subinstr("`name'", "logR", "",.)
	local name3 = subinstr("`name2'", "GTQ", "1_GTQ",.)
	local name4 = subinstr("`name3'", "prepr", "2_prepr",.)
	local name5 = subinstr("`name4'", "pimpl", "3_pimpl",.)
	esttab using G`name5'_SW.rtf, order(z_GiniWealth z_MeanGiniSWIID z_logMeanGDP z_meanUDS c.z_GiniWealth#c.z_logMeanGDP c.z_GiniWealth#c.z_meanUDS ///
	c.z_MeanGiniSWIID#c.z_logMeanGDP c.z_MeanGiniSWIID#c.z_meanUDS c.z_GiniWealth#c.z_MeanGiniSWIID) b(3) se(3) r2 varwidth(12) compress label ///
	nobaselevels nogaps noomitted noconstant interaction(" \f1 x \f0") ///
	title(Stepwise pooled cross-sectional models of `"`: var label `var''"') ///
	nonumbers mtitles("Model 1 Coeff. (SE)" "Model 2 Coeff. (SE)" "Model 3 Coeff. (SE)" "Model 4 Coeff. (SE)" "Model 5 Coeff. (SE)" "Model 6 Coeff. (SE)" "Model 7 Coeff. (SE)" "Model 8 Coeff. (SE)" "Model 9 Coeff. (SE)" "Model 10 Coeff. (SE)") ///
	nonotes addnotes("Note.---Coeff. = Coefficient; SE = Robust standard errors clustered at country level; GDP: Gross Domestic Product per capita at purchasing power parity 2017 international $; UDS: Unified Democracy Score; GDP and Social Trust transformed using natural logarithm, all variables z-standardized. Constant is not displayed * p < 0.05, ** p < 0.01, *** p < 0.001.") ///
	fonttbl(\f0\fnil Times New Roman;\f1\fnil Arial;) substitute("\fs20" "\fs22" "Interval=" "Interval " "Observations" "\i N" " p " "{\i p }" "*" "{\super *}") replace
}

//manually format the SW tables: (1) landscape, (2) autofit to content, (3) widen most left column, (4) narrow most right column, (5) distribute columns evenly
//manually remove "and Social Trust" when no Social Trust in table (concerns comment abt log transformation).
